package com.gitee.wsl.compose.chart.polar

import androidx.compose.animation.core.AnimationSpec
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.unit.dp
import com.gitee.wsl.common.ui.draw.style.AreaStyle
import com.gitee.wsl.common.ui.draw.style.LineStyle
import com.gitee.wsl.compose.chart.Chart
import com.gitee.wsl.compose.chart.ChartLayout
import com.gitee.wsl.compose.chart.ExperimentalKoalaPlotApi
import com.gitee.wsl.compose.chart.KoalaPlotTheme
import com.gitee.wsl.compose.chart.generateHueColorPalette
import com.gitee.wsl.compose.chart.legend.Legend
import com.gitee.wsl.compose.chart.legend.LegendLocation
import com.gitee.wsl.compose.chart.legend.LegendSlate
import com.gitee.wsl.compose.chart.legend.defaultLegend
import com.gitee.wsl.compose.chart.polar.axis.AngularAxisModel
import com.gitee.wsl.compose.chart.polar.axis.FloatRadialAxisModel
import com.gitee.wsl.compose.chart.polar.model.PolarPoint
import com.gitee.wsl.compose.chart.polar.style.PolarGraphDefaults
import com.gitee.wsl.compose.chart.polar.style.PolarGraphStyle
import com.gitee.wsl.compose.ui.widget.Symbol

@OptIn(ExperimentalKoalaPlotApi::class)
@Composable
fun <T> Chart.Polar(
    modifier: Modifier = Modifier,
    title: @Composable () -> Unit = {},
    legend: LegendSlate = Legend.defaultLegend,
    legendLocation: LegendLocation = KoalaPlotTheme.legendLocation,

    radialAxisModel: FloatRadialAxisModel,
    angularAxisModel: AngularAxisModel<T>,
    radialAxisLabelText: (Float) -> String = radialAxisModel::label,
    angularAxisLabelText: (T) -> String = angularAxisModel::label,
    polarGraphStyle: PolarGraphStyle = PolarGraphDefaults.Default(),

    data: List<List<PolarPoint<Float, T>>>,
    palette: List<Color> = remember { generateHueColorPalette(count = data.size) },
    lineStyle: ((Int) -> LineStyle?)? = {index-> LineStyle(SolidColor(palette[index]), strokeWidth = 1.5.dp) },
    areaStyle: ((Int) -> AreaStyle?)? = null,
    animationSpec: AnimationSpec<Float> = KoalaPlotTheme.animationSpec,
    symbols: PolarSymbolSlate<T>? = null,
){
    ChartLayout(
        modifier = modifier,
        title = title,
        legend = legend,
        legendLocation = legendLocation
    ) {
        PolarGraph(
            radialAxisModel = radialAxisModel,
            angularAxisModel = angularAxisModel,
            radialAxisLabelText=radialAxisLabelText,
            angularAxisLabelText=angularAxisLabelText,
            polarGraphStyle=polarGraphStyle
        ){
            data.forEachIndexed { index, seriesData ->
                PolarPlotSeries(
                    seriesData,
                    lineStyle = lineStyle?.invoke(index),
                    areaStyle = areaStyle?.invoke(index),
                    symbols = symbols,
                    animationSpec = animationSpec
                )
            }
        }
    }
}
